package org.kailu.book.controller;

import org.kailu.book.entity.CategoryEntity;
import org.kailu.book.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author viet.ch
 * @generated May 24, 2014
 * @from VN
 */

@Controller
public class CategoryController {

	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping("category")
	private String getData(ModelMap map) {
		map.addAttribute("categoryList", categoryService.getAll());
		return "category";
	}
	
	@RequestMapping(value = "/category/saveorupdate", method = RequestMethod.GET)
	public String initData(ModelMap map) {
		map.addAttribute("category",new CategoryEntity());
		return "category_new";
	}
	
	@RequestMapping(value = "/category/saveorupdate", method = RequestMethod.POST)
    public String add(@ModelAttribute(value="category") CategoryEntity categoryEntity, BindingResult result, ModelMap map)
    {
		categoryService.saveOrUpdate(categoryEntity);
		return "redirect:/category";
    }
	
	@RequestMapping(value = "/category/saveorupdate/{categoryId}", method = RequestMethod.GET)
    public String update(@PathVariable("categoryId") int categoryId, ModelMap map)
    {
		map.addAttribute("category", categoryService.get(categoryId));
		return "category_new";
    }
	
	@RequestMapping(value = "/category/delete/{categoryId}", method = RequestMethod.GET)
    public String delete(@PathVariable("categoryId") int categoryId)
    {
		this.categoryService.delete(categoryId);
		return "redirect:/category";
    }
	
}
